import * as Cesium from "cesium";

class SimpleLabel {
    constructor(viewer, options) {
        // 初始化参数
        this.viewer = viewer;
        this.position = options.position;
        this.label = options.label;
        this.isShow = options.isShow !== undefined ? options.isShow : true;
        this.color = options.color || "#ff0000";
        this.scaleByDistance = options.scaleByDistance;
        
        // 创建标签实体
        this.labelEntity = this.viewer.entities.add({
            position: this.position,
            
            label: {
                text: this.label,
                font: '18px sans-serif',
                fillColor: Cesium.Color.fromCssColorString(this.color),
                outlineColor: Cesium.Color.WHITE,
                outlineWidth: 2,
                pixelOffset: new Cesium.Cartesian2(0, 10), // 标签偏移
                scaleByDistance: this.scaleByDistance,
            }
        });

        // 控制标签的显示
        this.setVisible(this.isShow);

        // 记录实例
        this.vmInstance = { el: this.labelEntity }; // 模拟 Vue 实例
    }

    // 添加标签到视图
    async addLabel() {
        // 这里可以执行任何初始化逻辑
        console.log(`Label ${this.label} has been added at position (${this.position.lng}, ${this.position.lat}).`);
        this.setVisible(this.isShow);
    }

    // 设置标签的可见性
    setVisible(isVisible) {
        this.labelEntity.label.show = isVisible;
    }

    // 移除标签
    removeMarker() {
        this.viewer.entities.remove(this.labelEntity);
        console.log(`Label ${this.label} has been removed.`);
    }

    // 更新标签文本
    updateLabel(newLabel) {
        this.labelEntity.label.text = newLabel;
    }

    // 更新位置
    updatePosition(newPosition) {
        this.position = newPosition;
        this.labelEntity.position = Cesium.Cartesian3.fromDegrees(this.position.lng, this.position.lat);
    }
}

// 导出 SimpleLabel 类
export default SimpleLabel;
